<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { getEvaluateTableDataApi } from "@/api/table"
import { GetEvaluateTableData } from "@/api/table/types/table"
import { ElMessage, ElMessageBox, type FormInstance } from "element-plus"
import { Refresh, RefreshRight, Search } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import * as XLSX from "xlsx"
import {useUserStore} from "@/store/modules/user";
defineOptions({
  // 命名当前组件
  name: "works"
})
const userStore=useUserStore()

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

//#region 查
const tableData = ref<GetEvaluateTableData[]>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  judgeName: "",
  joinTeam: "",
  workName: "",
  workTeam: "",
  joinSchool: ""
})
const getTableData = () => {
  loading.value = true
  getEvaluateTableDataApi({
    current: paginationData.currentPage,
    pageSize: paginationData.pageSize,
    joinTeam: searchData.joinTeam || undefined,
    workName: searchData.workName || undefined,
    judgeName: searchData.judgeName || undefined,
    joinSchool: searchData.joinSchool || undefined,
    workTeam: searchData.workTeam || undefined
  })
    .then((res) => {
      paginationData.total = parseInt(res.data.total)
      tableData.value = res.data.records
      ElMessage.success("查询成功")
    })
    .catch(() => {
      tableData.value = []
      ElMessage.error("查询失败")
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
}
const resetSearch = () => {
  ;(searchData.joinTeam = ""),
    (searchData.judgeName = ""),
    (searchData.workName = ""),
    (searchData.joinSchool = ""),
    (searchData.workTeam = ""),
    handleSearch()
}
//#endregion

const options = [
  {
    value: "",
    label: ""
  },
  {
    value: "1",
    label: "思政科组"
  },
  {
    value: "2",
    label: "公共基础课程组(不含思政)"
    // disabled: true
  },
  {
    value: "3",
    label: "专业技能课程一组"
  },
  {
    value: "4",
    label: "专业技能课程二组"
  }
]
const webDerive = () => {
  ElMessageBox.confirm("是否确认导出选中数据?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    const tableHeader = [
      [
        "学校",
        "作品名称",
        "评委姓名",
        "教学实施方案",
        "教案",
        "视频资料",
        "专业人才培养方案",
        "课程标准",
        "教材选用",
        "得分",
        "评分时间"
      ] //导出表头
    ] // 表格表头
    tableData.value.forEach((item: any, index) => {
      let rowData = []
      //导出内容的字段
      rowData = [
        item.joinSchool,
        item.workName,
        item.teachingReport,
        item.lessonPlan,
        item.classroomRecording,
        item.talentDevelopmentPlan,
        item.courseStandard,
        item.textbookSelectionNote,
        item.comprehensiveScore,
        item.scoreTime
      ]
      tableHeader.push(rowData), index + 1
    })
    const workSheet = XLSX.utils.aoa_to_sheet(tableHeader)
    const bookNew = XLSX.utils.book_new()
    // 定义所有列的宽度
    const columnWidth = 20 // 你可以根据需要修改列宽度
    // 设置所有列的宽度
    workSheet["!cols"] = Array(tableHeader[0].length).fill({ wch: columnWidth })
    XLSX.utils.book_append_sheet(bookNew, workSheet, "评价信息") // 工作簿名称
    const name = "评价管理管理数据" + ".xlsx"
    XLSX.writeFile(bookNew, name) // 保存的文件名
  })
}
const generateTargetPageURL = (dynamicId) => {
  // 构建包含动态参数的 URL
  return `http://localhost:5173/worksList?id=${dynamicId.id}&userId=${dynamicId.userId}&adminId=${userStore.getLocalUserInfo().id}`
}
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item label="评委">
          <el-input v-model="searchData.judgeName" placeholder="请输入评委" />
        </el-form-item>
        <el-form-item label="作品名称">
          <el-input v-model="searchData.workName" placeholder="请输入作品名称" />
        </el-form-item>
        <el-form-item label="学校">
          <el-input v-model="searchData.joinSchool" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="参赛人员">
          <el-input v-model="searchData.joinTeam" placeholder="请输入参赛人员" />
        </el-form-item>
        <el-form-item label="报名组别">
          <el-select v-model="searchData.workTeam" placeholder="请选择" style="width: 240px">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-button type="success" icon="download" @click="webDerive">导出数据</el-button>
        </div>
        <div>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" @click="getTableData">刷新</el-button>
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column prop="joinSchool" label="学校" align="center" />
          <el-table-column prop="workName" label="作品名称" align="center" />
          <el-table-column prop="judgeName" label="评委姓名" align="center" />
          <el-table-column prop="teachingReport" label="教学实施报告" align="center" />
          <el-table-column prop="lessonPlan" label="教案" align="center" />
          <el-table-column prop="classroomRecording" label="视频资料" align="center" />
          <el-table-column prop="talentDevelopmentPlan" label="专业人才培养方案" align="center" />
          <el-table-column prop="courseStandard" label="课程标准" align="center" />
          <el-table-column prop="textbookSelectionNote" label="教材选用说明" align="center" />
          <el-table-column prop="comprehensiveScore" label="评分" align="center" />
          <el-table-column prop="scoreTime" label="评分时间" align="center" />
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <a :href="generateTargetPageURL(scope.row)" target="_blank">
                <el-button icon="editPen" type="primary" text bg size="small">查看</el-button>
              </a>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
